package com.fastloan.jn.mgt.application.basic.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.application.basic.dao.JnApplicationFileMapper;
import com.fastloan.jn.mgt.application.basic.dao.JnApplicationMapper;
import com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus;
import com.fastloan.jn.mgt.application.basic.model.JnApplication;
import com.fastloan.jn.mgt.application.basic.model.JnApplicationFile;
import com.fastloan.jn.mgt.application.basic.model.vo.AppCustomerExt;
import com.fastloan.jn.mgt.application.basic.model.vo.AppExt;
import com.fastloan.jn.mgt.application.basic.model.vo.JnApplicationVo;
import com.fastloan.jn.mgt.application.basic.service.JnApplicationService;
import com.kawins.supers.file.core.FileLoadBean;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * Created by gelon on 2017/10/23.
 */
@Service
public class JnApplicationServiceImpl extends BasicServiceImpl<JnApplication> implements JnApplicationService {

    @Autowired
    private JnApplicationMapper applicationMapper;

    @Autowired
    private IFileManager fileManager;

    @Autowired
    private JnApplicationFileMapper jnApplicationFileMapper;

    @Override
    @DataTable(name = "appCustomerExt")
    public List<AppCustomerExt> appCustomerExt(Map<String, Object> map) {
        return applicationMapper.queryAppCustomer(map);
    }

    @DataTable(name = "selectAllAppExt")
    public List<AppExt> selectAllAppExt(Map<String, Object> map) {
        return applicationMapper.selectAllAppExt(map);
    }

    @Override
    @DataTable(name = "selectApplicationList")
    public List<JnApplicationVo> selectApplicationList(Map<String, Object> map) {
        return applicationMapper.selectApplicationList(map);
    }

    @Override
    public void uploadFile(String applicationId, MultipartFile[] files) {
        List<FileLoadBean> upload = fileManager.upload(files);
        upload.forEach(fileLoadBean -> jnApplicationFileMapper
                .insert(new JnApplicationFile(applicationId, 0, fileLoadBean.getFileId().toString())));
    }

    @Override
    @DataTable(name = "selectApplicationFileList")
    public List<JnApplicationVo> selectApplicationFileList(Map<String, Object> map) {
        return applicationMapper.selectApplicationFileList(map);
    }

    @Override
    @Transactional
    public boolean modifyApplicationStatus(ApplicationStatus applicationStatus, String applicationId) {
        JnApplication application = selectByPrimaryKey(applicationId);
        application.setApplicationStatus(applicationStatus.getType());
        Integer result = updateByPrimaryKeySelective(application);
        return result == 1;

    }
}
